TargetArticle targetDoc;
SourceArticle sourceDoc;

boolean sourceClicked;

int totalWidth = 1024;
int totalHeight = 600;

int pgDocumentsWidth = (int) (0.65 * totalWidth);
int pgPreviewWidth = totalWidth - pgDocumentsWidth;

// width and height % 
int documentWidth = (int)(0.38 * pgDocumentsWidth);
int documentHeight = (int)(0.9 * totalHeight);

int documentPaddingX = (int)(pgDocumentsWidth - (2 * documentWidth))/3;
int documentPaddingY = (int)(totalHeight - documentHeight)/2;

PGraphics pgDocuments, pgPreview;

PFont font;

String showingContent = "";
int textWidth = 60;

public void setup() {
  // Size of the canvas
  size(totalWidth, totalHeight);

  pgDocuments = createGraphics(pgDocumentsWidth, totalHeight, JAVA2D);
  pgPreview = createGraphics(pgPreviewWidth, totalHeight, JAVA2D);

  // An example is created, with two fixed documents

  // Document 1
  targetDoc = new TargetArticle(documentWidth, documentHeight, pgDocuments, pgPreview);

  targetDoc.addNewSection(new Section("Texto de la Sección 1. Esta sección es medio corta."));
  targetDoc.addNewSection(new Section("Texto de la Sección 2. Esta sección es larga. Esperemos a ver que funcione bien lo que quiero hacer, y en fin, ojalá todo salga bien este semestre y este año."));
  targetDoc.addNewSection(new Section("Texto de la Sección 3."));
  targetDoc.addNewSection(new Section("Texto de la Sección 4. Esta es una sección media."));

  targetDoc.setSectionsColor(color(0, 196, 0));

  targetDoc.setArticlePosition(documentPaddingX, documentPaddingY);

  // Document 2
  sourceDoc = new SourceArticle(documentWidth, documentHeight, pgDocuments, pgPreview);

  sourceDoc.addNewSection(new Section("Texto de la Sección 1. Esta sección es medio corta."));
  sourceDoc.addNewSection(new Section("Texto de la Sección 2."));
  sourceDoc.addNewSection(new Section("Texto de la Sección 3. Esta es una sección media, pero no tan tan tan tan tan media."));
  sourceDoc.addNewSection(new Section("Texto de la Sección 4. Esta es una sección largalargalargalargalargalargalargalargalargalargalargalargalargalargalargalargalargalargalargalargalargalargalargalargalargalargalarga."));
  sourceDoc.addNewSection(new Section("Texto 5."));

  sourceDoc.setSectionsColor(color(0, 0, 128));

  sourceDoc.setArticlePosition(((2 * documentPaddingX) + documentWidth), documentPaddingY);

  // The system status is reset (initialized)
  restartSystemStatus();

  // The font is initialized
  font = loadFont("data/font.vlw");

  pgDocuments.textFont(font);
  pgPreview.textFont(font);
}

public void draw() {
  // Documents area
  pgDocuments.beginDraw();

  pgDocuments.background(212);

  pgDocuments.fill(128);
  pgDocuments.textAlign(CENTER, CENTER);
  pgDocuments.text("TARGET", (documentPaddingX + (documentWidth / 2)), (documentPaddingY / 2));
  pgDocuments.text("SOURCE", ((2 * documentPaddingX) + documentWidth + (documentWidth / 2)), (documentPaddingY / 2));

  targetDoc.draw();
  sourceDoc.draw();

  pgDocuments.endDraw();

  // Preview Section Area
  pgPreview.beginDraw();

  pgPreview.background(212);

  pgPreview.fill(128);
  pgPreview.textAlign(CENTER, CENTER);
  pgPreview.text("SECTION PREVIEW", (float)(pgPreviewWidth / 2), (float)(0.1 * totalHeight));

  if (targetDoc.isMouseOverAnySection()) {
    showingContent = targetDoc.getTextFromSectionIn(mouseX, mouseY);
  }
  else {
    if (sourceDoc.isMouseOverAnySection()) {
      showingContent = sourceDoc.getTextFromSectionIn(mouseX, mouseY);
    }
    else {
      showingContent = "";
    }
  }		
  showText();

  pgPreview.endDraw();

  // The PGraphics are added to the PApplet
  image(pgDocuments, 0, 0);
  image(pgPreview, pgDocumentsWidth, 0);
}

private void showText() {
  int contentLength = showingContent.length();

  int i = 0;
  int j = i + textWidth;

  String newContent = "";

  if (j < contentLength) {
    while (j < contentLength) {
      if (showingContent.charAt(j) != ' ') {
        while (showingContent.charAt (j) != ' ' && (j > i)) {
          j--;
        }
      }

      if (j <= i) {
        j = i + textWidth;
      }

      newContent = newContent + showingContent.substring( i, j ) + "\n";				

      i = j;
      j = i + textWidth;
    }

    newContent = newContent + showingContent.substring(i - 1);
  }
  else {
    newContent = showingContent;
  }

  pgPreview.fill(0);
  pgPreview.textAlign(LEFT);
  pgPreview.textLeading(20);
  pgPreview.text(newContent, 0, (float)(0.15 * totalHeight));
}

/**
 	 * When the mouse is clicked, it is asked to the documents if they have 'received' the click, 
 	 * and then the text of that section is showed
 	 */
public void mouseClicked() {
  if (mouseButton == RIGHT) {
    if (targetDoc.isMouseOverAnySection()) {
      targetDoc.removeTranscludedSection();
    }
  }
}

/**
 	 * This method checks when a click is made on any Document
 	 * Checks only when the document is a source document 
 	 */
public void mousePressed() {
  if (sourceDoc.isMouseOverAnySection()) {
    sourceClicked = true;
  }
  else {
    sourceClicked = false;
  }
}

/**
 	 * When the mouse is pressed and dragged, it is asked to the documents if it was pressed on a section. 
 	 * If the document is a 'source' one, it allows to 'drag' the selected section.
 	 * If the document is a 'target' one, it DOES NOT allow to drag the selected section.
 	 */
public void mouseDragged() {
  if (sourceClicked) {
    if (!sourceDoc.hasSectionSelected()) { 	//If a section is already dragged, don't highlight the other
      sourceDoc.selectPressedSection(mouseX, mouseY);
    }

    setTransclusionStatus(true);
  }
}

/**
 	 * Method that helps with the transclusion status settings
 	 * This method is internal in this class 
 	 * @param status
 	 */
private void setTransclusionStatus(boolean tStatus) {
  targetDoc.setTransclusionStatus(tStatus);
  sourceDoc.setTransclusionStatus(tStatus);
}

/**
 	 * When the mouse is released, it is asked to the source document to 'release' the section
 	 * If the section lands between two sections of the 'target' document, a new transclusion is made
 	 */
public void mouseReleased() {
  Section tempSection = null;
  Section tempPressedSection = null;
  int position;

  // The section is determined and copied
  if (sourceDoc.hasSectionSelected()) { //If a section is already dragged
    tempPressedSection = sourceDoc.getPressedSection(mouseX, mouseY);
    tempSection = sourceDoc.releasePressedSection(mouseX, mouseY);
  }

  // The copied section is added to the target document
  if (targetDoc.hasTwoSectionsSelected()) {
    if (tempSection != null) {
      position = targetDoc.getSelectedSectionsPosition();

      targetDoc.addNewTranscludedSection(tempSection, tempPressedSection, position);

      setTransclusionStatus(false);
    }
  }

  // The applet status is reinitialized
  restartSystemStatus();
}

/**
 	 * Method for internal use only: method that reset the status on the documents, to start a new transclusion
 	 */
private void restartSystemStatus() {
  sourceClicked = false;

  targetDoc.resetStatus();
  sourceDoc.resetStatus();		

  setTransclusionStatus(false);
}	






























































public  class Article {
  // Sections of text
  protected ArrayList<Section> sections;

  // Coordinates (x, y, width, height) 
  protected float articleX;
  protected float articleY;
  protected float articleW; 
  protected float articleH;

  protected float drawingArea;

  // The padding of the drawing object (to avoid visual collisions with the sections)
  protected int sectionPadding = 7;

  // Indicator: the current status of the system (transcluding or not transcluding)
  protected boolean inTransclusion;	

  // PGraphics parent
  protected PGraphics pgDocuments;
  protected PGraphics pgPreview;

  /**
   	 * Constructor with parameter width and NO position
   	 * @param articleWidth
   	 * @param parent PApplet 
   	 */
  public Article(float articleWidth, float articleHeight, PGraphics pgDocuments, PGraphics pgPreview) {
    sections = new ArrayList<Section>();

    articleW = articleWidth;

    articleH = articleHeight;

    this.pgDocuments = pgDocuments;

    this.pgPreview = pgPreview;
  }

  /**
   	 * DRAW Method.
   	 * It is mandatory to set the position of the document BEFORE using this method
   	 */
  public void draw() {
    // TODO: Maybe delete? this color assignment
    pgDocuments.fill(196);

    // The rectangle over the document is drawn		
    pgDocuments.stroke(64);
    pgDocuments.rect (articleX, articleY, articleW, articleH);
    pgDocuments.stroke(0);

    // Now the sections are drawn, one by one
    drawSections();
  }

  /**
   	 * Method that draws the sections of this Document.
   	 * 		The position is calculated with these rules:
   	 * 			posX = posXDocument + padding
   	 * 
   	 * 			posY0 = posYDocument + padding
   	 * 			posYN = posY(N-1) + padding
   	 * 		THE PADDING IS ADDED BY THE DRAW METHOD OF THE SECTION!!!!
   	 */
  private void drawSections() {
    calculateSectionsSize();

    int numberOfSections = sections.size();
    Section tempSection;

    for (int i=0; i<numberOfSections; i++) {
      tempSection = (Section)sections.get(i);

      // The PGraphcs are given to the Sections
      tempSection.setPAppletAndPGraphics(pgDocuments, pgPreview);

      // (INSIDE THE CLASS, it is checked for the mouse position, to know when to change color)
      tempSection.draw();
    }
  }

  /**
   	 * Method that determines the size of the document, according to the sections size
   	 * The size of the document equals to:
   	 * 		Document = (Section1 + P1) + ... + (SectionK +Pk) + P(k+1)
   	 * 	Letting P be the padding between sections
   	 */
  private void calculateSectionsSize() {
    int numberOfSections = sections.size();
    Section tempSection;

    drawingArea = articleH - ((numberOfSections + 1) * sectionPadding);

    float sectionX, sectionY, sectionW, sectionH;

    float sumOfSectionSize = 0;

    // Initial coordinates and dimensions
    sectionX = articleX + sectionPadding;
    sectionY = articleY + sectionPadding;
    sectionW = articleW - (2 * sectionPadding); // The document width minus the 'margins'

    for (int i=0; i<numberOfSections; i++) {
      tempSection = (Section)sections.get(i);

      sumOfSectionSize = sumOfSectionSize + tempSection.calculateSectionHeight();
    }

    for (int i=0; i<numberOfSections; i++) {
      tempSection = (Section)sections.get(i);

      sectionH = (tempSection.calculateSectionHeight() / sumOfSectionSize) * drawingArea;

      tempSection.setSectionDimensions(sectionX, sectionY, sectionW, sectionH);

      // The new Y is determined
      sectionY = sectionY + tempSection.getSectionSize() + sectionPadding;
    }
  }

  /**
   	 * Method that adds a Section to the current document
   	 * It sets the PApplet and PGraphics elements
   	 * @param section
   	 */
  public void addNewSection(Section section) {
    section.setPAppletAndPGraphics(pgDocuments, pgPreview);

    sections.add(section);
  }

  public void setTransclusionStatus(boolean tStatus) {
    this.inTransclusion = tStatus;
  }

  /**
   	 * Method that checks if a click was made on this document, specifically ON THE SECTIONS.
   	 * @return
   	 */
  public boolean isMouseOverAnySection() {
    int numberOfSections = sections.size();
    boolean isOver, result;

    result = false;

    Section tempSection;

    for (int i=0; i<numberOfSections; i++) {
      tempSection = (Section)sections.get(i);

      isOver = tempSection.checkForMouseOver();

      if (isOver) {
        result = isOver;
      }
    }

    return result;
  }


  /**
   	 * Method that sets the position of drawing of a document
   	 * @param posX
   	 * @param 
   	 */
  public void setArticlePosition(float posX, float posY) {
    articleX = posX;
    articleY = posY;
  }

  /**
   	 * Method that checks if there is a section that was pressed
   	 * @param posX mouse position in x axis
   	 * @param posY mouse position in y axis
   	 */
  public String getTextFromSectionIn(int posX, int posY) {
    int numberOfSections = sections.size();
    boolean isOver = false;
    Section tempSection;
    String sectionContent = "";

    for (int i=0; i<numberOfSections; i++) {
      tempSection = (Section)sections.get(i);

      isOver = tempSection.checkForMouseOver();

      if (isOver) {
        sectionContent = tempSection.getSectionText();
      }
    }

    return sectionContent;
  }
}





























public class SourceArticle extends Article {
  // Indicator if a Section of this document is selected
  private boolean sectionSelected;

  public SourceArticle(float f, float h, PGraphics pgParent, PGraphics pgPreview) {
    super(f, h, pgParent, pgPreview);

    int numberOfSections = this.sections.size();
    Section tempSection = null;

    for (int i=0; i<numberOfSections; i++) {
      tempSection = (Section)this.sections.get(i);

      tempSection.setMovable(true);
    }
  }


  /**
   	 * DRAW Method.
   	 * It is mandatory to set the position of the document BEFORE using this method
   	 */
  public void draw() {
    super.draw();

    drawSections();
  }

  /**
   	 * Method that draws the sections of this Document.
   	 * 		The position is calculated with these rules:
   	 * 			posX = posXDocument + padding
   	 * 
   	 * 			posY0 = posYDocument + padding
   	 * 			posYN = posY(N-1) + padding
   	 * 		THE PADDING IS ADDED BY THE DRAW METHOD OF THE SECTION!!!!
   	 */
  private void drawSections() {
    int numberOfSections = this.sections.size();
    Section tempSection;

    if (inTransclusion) {
      int sX, sY, sW, sH;

      sW = sectionPadding * 6;
      sH = sectionPadding * 4;

      sX = mouseX - (sW/2);
      sY = mouseY - (sH/2);

      sectionSelected = false;	

      for (int i=0; i<numberOfSections; i++) {
        tempSection = (Section)sections.get(i);

        if (tempSection.isSelected()) {
          sectionSelected = true;
        }
      }

      // If a section on this document is selected, a rectangle is drawn
      if (sectionSelected) {
        pgDocuments.fill(128);
        pgDocuments.rect(sX, sY, sW, sH);
      }
    }
  }

  /**
   	 * This method sets the color for all sections in the document
   	 * @param newColor
   	 */
  public void setSectionsColor(int newColor) {
    int numberOfSections = sections.size();
    Section tempSection;

    for (int i=0; i<numberOfSections; i++) {
      tempSection = (Section)sections.get(i);

      tempSection.setSectionColor(newColor);
    }
  }


  /**
   	 * Method that checks and 'selects' the selected (pressed) section
   	 * @param posX mouse position in x axis
   	 * @param posY mouse position in y axis
   	 */
  public void selectPressedSection(int posX, int posY) {
    int numberOfSections = sections.size();
    boolean isOver = false;
    Section tempSection;

    for (int i=0; i<numberOfSections; i++) {
      tempSection = (Section)sections.get(i);

      isOver = tempSection.checkForMouseOver();

      if (isOver) {
        tempSection.selectSection();
      }
    }

    // Disable the functionality of highlighting of sections.
    this.setHighlightStatus(false);
  }

  /**
   	 * 
   	 * @param posX
   	 * @param posY
   	 * @return 
   	 */
  public Section releasePressedSection(int posX, int posY) {
    int numberOfSections = sections.size();
    boolean isSelected = false;
    Section tempSection, returnSection;

    returnSection = null;

    for (int i=0; i<numberOfSections; i++) {
      tempSection = (Section)sections.get(i);

      isSelected = tempSection.isSelected();

      if (isSelected) {
        returnSection = tempSection.releaseSection();
      }
    }

    this.setHighlightStatus(true);

    return returnSection;
  }


  public boolean hasSectionSelected() {
    return this.sectionSelected;
  }

  /**
   	 * Method that enables or disables the highlighting function on this document (SOURCE).
   	 * @param status
   	 */
  private void setHighlightStatus(boolean tStatus) {
    int numberOfSections = sections.size();

    Section tempSection;

    for (int i=0; i<numberOfSections; i++) {
      tempSection = (Section)sections.get(i);

      tempSection.setHighlightStatus(tStatus);
    }
  }

  public void resetStatus() {
    int numberOfSections = sections.size();

    Section tempSection;

    for (int i=0; i<numberOfSections; i++) {
      tempSection = (Section)sections.get(i);

      tempSection.setHighlightStatus(true);
      tempSection.setForceHighlight(false);
    }
  }


  public Section getPressedSection(int mouseX, int mouseY) {
    int numberOfSections = sections.size();
    boolean isSelected = false;
    Section tempSection, returnSection;

    returnSection = null;

    for (int i=0; i<numberOfSections; i++) {
      tempSection = (Section)sections.get(i);

      isSelected = tempSection.isSelected();

      if (isSelected) {
        returnSection = tempSection.getSection();
      }
    }

    return returnSection;
  }
}

















































public class TargetArticle extends Article {
  // Indicator: when 2 sections of this document are selected, this flag is 'on'
  private boolean twoSectionsSelected;

  public TargetArticle(float aWidth, float aHeight, PGraphics pgParent, PGraphics pgPreview) {
    super(aWidth, aHeight, pgParent, pgPreview);

    int numberOfSections = this.sections.size();
    Section tempSection = null;

    for (int i=0; i<numberOfSections; i++) {
      tempSection = (Section)this.sections.get(i);

      tempSection.setMovable(false);
    }
  }


  /**
   	 * DRAW Method.
   	 * It is mandatory to set the position of the document BEFORE using this method
   	 * @param pgDocuments 
   	 * @param pgDocuments 
   	 */
  public void draw() {
    super.draw();

    drawSections();

    highlightParentSectionsIfMouseOver();
  }

  /**
   	 * Method that highlights a parent section if there is on transcludedSection witha  mouse over
   	 */
  private void highlightParentSectionsIfMouseOver() {
    int numberOfSections = this.sections.size();
    Section tempSection = null;

    Section mouseOverSection = null;

    for (int i=0; i<numberOfSections; i++) {
      tempSection = (Section)this.sections.get(i);

      if (tempSection.checkForMouseOver()) {
        if (tempSection.isTranscluded()) {
          mouseOverSection = tempSection;
        }
      }
    }

    if (mouseOverSection != null) {
      if (mouseOverSection.getParentSection() != null) {
        mouseOverSection.getParentSection().setForceHighlight(true);
      }
    }
    else {
      for (int i=0; i<numberOfSections; i++) {
        tempSection = (Section)this.sections.get(i);

        if (tempSection.isTranscluded()) {
          if (tempSection.getParentSection() != null) {
            tempSection.getParentSection().setForceHighlight(false);
          }
        }
      }
    }
  }


  /**
   	 * Method that adds some functionalities to the drawSections method in Article.
   	 */
  private void drawSections() {
    int numberOfSections = this.sections.size();
    Section tempSection;

    if (inTransclusion) {
      int counter;

      // If is a target doc, the sections must react to the size of the rectangle
      for (int i=0; i<numberOfSections; i++) {
        tempSection = (Section)sections.get(i);

        tempSection.setRectangleSensitivity(true, sectionPadding);

        // TODO: Check when 2 sections are selected and change the rectangle color
        counter = this.getNumberOfSelectedSections();

        if (counter > 1) {
          twoSectionsSelected = true;
        }
        else {
          twoSectionsSelected = false;
        }
      }
    }
  }


  /**
   	 * This method sets the color for all sections in the document
   	 * @param newColor
   	 */
  public void setSectionsColor(int newColor) {
    int numberOfSections = sections.size();
    Section tempSection;

    for (int i=0; i<numberOfSections; i++) {
      tempSection = (Section)sections.get(i);

      tempSection.setSectionColor(newColor);
    }
  }

  public boolean hasTwoSectionsSelected() {
    return this.twoSectionsSelected;
  }

  public int getSelectedSectionsPosition() {
    int numberOfSections = sections.size();
    boolean isOver = false;
    Section tempSection;

    int position = 0;

    for (int i=0; i<numberOfSections; i++) {
      tempSection = (Section)sections.get(i);

      isOver = tempSection.checkForMouseOver();

      if (isOver) {
        position = i;
      }
    }

    return position;
  }



  private int getNumberOfSelectedSections() {
    int numberOfSections = sections.size();

    int sectionCounter = 0;

    Section tempSection;

    for (int i=0; i<numberOfSections; i++) {
      tempSection = (Section)sections.get(i);

      if (tempSection.checkForMouseOver()) {
        sectionCounter++ ;
      }
    }

    return sectionCounter;
  }

  /**
   	 * Method that adds a section in the specified position (TARGET)
   	 * @param newSection
   	 * @param position
   	 */
  public void addNewTranscludedSection(Section newSection, Section parent, int position) {
    newSection.setTranscluded(true, parent);

    sections.add(position, newSection);
  }

  public void resetStatus() {
    int numberOfSections = sections.size();

    Section tempSection;

    for (int i=0; i<numberOfSections; i++) {
      tempSection = (Section)sections.get(i);

      tempSection.setRectangleSensitivity(false, sectionPadding);
    }
  }

  public void removeTranscludedSection() {
    int numberOfSections = sections.size();

    Section tempSection;

    for (int i=0; i<numberOfSections; i++) {
      tempSection = (Section)sections.get(i);

      if (tempSection.checkForMouseOver()) {
        if (tempSection.isTranscluded()) {
          this.sections.remove(tempSection);

          i = numberOfSections;
        }
      }
    }
  }
}































// TODO: REVISAR que la Section no se encargue de colorearse ni nada, que eso sea a través de la Clase artículo. 

public class Section {
  // From the content itself
  private String content;
  private int contentColor;

  // positionX, positionY, and the width of this section is determined in the document class
  private float sectionX;
  private float sectionY;
  private float sectionW;
  private float sectionH;	// The height is an attribute calculated by the Article

  // If the section is currently with the mouse over or it is selected, this flag changes
  private boolean withMouseOver;

  // Variable that helps with the movable sections (From the SourceArticle)
  private boolean movable;

  // Variable that helps with the movable sections (From the SourceArticle)
  private boolean isTranscluded;

  // If the section was selected in the document, this flag changes
  private boolean isCurrentlyTranscluding;

  // Variable that controls (overrides) the highlighting status of this section (true highlight, false not)
  private boolean enableHighlight;
  private boolean forceHighlight;	// This variable overrides the highlighting

  // This is the 'parent' section, where this seciton was transcluded
  private Section parentSection;

  // Variable that helps when the rectangle is on the section
  private boolean sensitivity;

  // Variable just for calculate the sensitivity
  private int padding;

  // The parent PGraphics
  private PGraphics pgDocument;
  private PGraphics pgPreview;

  /**
   	 * Constructor
   	 * @param content
   	 * @param contentColor
   	 * @param parent: PApplet
   	 * @param pgParent: PGraphics
   	 * @param pgPreview: PGraphics
   	 */
  public Section(String content, int contentColor, PGraphics pgParent, PGraphics pgPreview) {
    // Sets variables defaults (null for objects, etc)
    initializeParameters();

    this.content = content;
    this.contentColor = contentColor;		

    this.pgDocument = pgParent;
    this.pgPreview = pgPreview;
  }

  /**
   	 * Default constructor: Just with content.
   	 * The objects and flags are basically initialized
   	 * @param content
   	 */
  public Section(String content) {
    this.content = content;

    initializeParameters();
  }

  private void initializeParameters() {
    this.contentColor = 0;

    this.withMouseOver = false;
    this.isCurrentlyTranscluding = false;
    this.enableHighlight = true;
    this.forceHighlight = false;

    this.pgDocument = null;
    this.pgPreview = null;
  }

  /**
   	 * Method that draws this section.
   	 * 		The position is calculated with these rules:
   	 * 			posX = posXDocument + padding
   	 * 
   	 * 			posY0 = posYDocument + padding
   	 * 			posYN = posY(N-1) + padding
   	 * 		THE POSITIONING RULES ARE USED IN THE DOCUMENT CLASS!!!!!
   	 */
  public void draw() {
    // Drawing color. It changes when the section is pressed
    int drawColor = contentColor;

    // Checks, before painting, if the mouse is over this section
    this.withMouseOver = checkForMouseOver();

    // Checks for pressed section
    if (isCurrentlyTranscluding) {
      drawColor = color(255, 0, 0);
    }

    // Sets the color of the section
    if (withMouseOver) {
      if ( !isCurrentlyTranscluding ) { // DO NOT Highlight the 'selected' section
        if ( enableHighlight ) {
          // TODO: Check how to change brightness
          pgDocument.fill(color(drawColor + 0x555));
        }
        else {
          pgDocument.fill(drawColor);
        }
      }
      else {
        pgDocument.fill(drawColor);
      }
    }
    else {
      if (forceHighlight) {
        if (enableHighlight) {
          pgDocument.fill(color(drawColor + 0x555));
        }
        else {
          pgDocument.fill(drawColor);
        }
      }
      else {
        pgDocument.fill(drawColor);
      }
    }

    // The rectangle over the document is drawn		
    pgDocument.stroke(64);
    pgDocument.rect (sectionX, sectionY, sectionW, sectionH);
    pgDocument.stroke(0);
  }

  /**
   	 * Method that sets the size and position of this section
   	 */
  public void setSectionDimensions(float sectionX2, float sectionY2, float sectionW2, float sectionH2) {
    this.sectionX = sectionX2;
    this.sectionY = sectionY2;
    this.sectionW = sectionW2;
    this.sectionH = sectionH2;
  }

  /**
   	 * Method that sets the size and position of this section
   	 */
  public float getSectionSize() {
    // TODO check when the section was transcluded
    return sectionH;
  }

  /**
   	 * Method that sets the section color
   	 * @param newColor
   	 */
  public void setSectionColor(int newColor) {
    this.contentColor =  newColor;
  }

  /**
   	 * Method that checks if this section has a mouse over, SectionX, Y and width are the dimensions of the Section
   	 * @return isOver: true if it is, otherwise false
   	 */
  public boolean checkForMouseOver() {
    boolean isOver = false;

    int posX, posY;

    posX = mouseX;
    posY = mouseY;

    if (sectionX < posX) {
      if ((sectionX + sectionW) > posX) { 
        // It is inside X axis
        if (sectionY < posY) {
          if ((sectionY + sectionH) > posY) {
            // It is in Y axis too
            isOver = true;
          }
        }
      }
    }

    // It is checked if the rectangle is existent
    if (sensitivity) {
      isOver = true;

      if (sectionX > (posX + (padding * 3))) {
        isOver = false;
      }

      if ((sectionX + sectionW) < (posX - (padding * 3))) {
        isOver = false;
      }

      if (sectionY > (posY + (padding * 2))) {
        isOver = false;
      }

      if ((sectionY + sectionH) < (posY - (padding * 2))) {
        isOver = false;
      }
    }

    return isOver;
  }

  /**
   	 * Method that prints on console the content of the section
   	 */
  public String getSectionText() {
    return content;
  }

  /**
   	 * Method that changes the color of the selected section, and creates a 'rectangle' to drag and drop (IN DRAW METHOD)
   	 */
  public void selectSection() {
    isCurrentlyTranscluding = true;
  }

  /**
   	 * Method that copies the selected section into a new section
   	 * @return Section: the new copied section.
   	 */
  public Section releaseSection() {
    // The section is un-selected
    isCurrentlyTranscluding = false;

    // The new Section is created
    Section tempSection = new Section(content, contentColor, pgDocument, pgPreview);

    return tempSection;
  }

  /**
   	 * Getter method
   	 * @return true or false, if is selected or not
   	 */
  public boolean isSelected() {
    return this.isCurrentlyTranscluding;
  }

  /**
   	 * Method that sets the highlight status of this section.
   	 * @param status: 	true, allows highlighting when the mouse is over
   	 * 					false, does not allow highlighting
   	 */
  public void setHighlightStatus(boolean tStatus) {
    this.enableHighlight = tStatus;
  }

  /**
   	 * Method that increases the sensitivity of the section with a mouse over (now working with a rectangle around the mouse)
   	 * @param sensitivity: true, enables sensitivity, false, not.
   	 * @param sectionPadding the padding of the document, used for the size of the rectangle.
   	 */
  public void setRectangleSensitivity(boolean sensitivity, int sectionPadding) {
    this.sensitivity = sensitivity;
    this.padding = sectionPadding;
  }

  public void setMovable(boolean movable) {
    this.movable = movable;
  }

  public boolean isMovable() {
    return movable;
  }

  public void setTranscluded(boolean isTranscluded, Section parent) {
    this.isTranscluded = isTranscluded;

    if (isTranscluded) {
      parentSection = parent;
    }
    else {
      parentSection = null;
    }
  }

  public boolean isTranscluded() {
    return isTranscluded;
  }

  public void setForceHighlight(boolean highlight) {
    this.forceHighlight = highlight;
  }

  public Section getSection() {
    return this;
  }

  public Section getParentSection() {
    Section tempSection = null;

    if (isTranscluded) {
      tempSection = parentSection;
    }

    return tempSection;
  }

  public float calculateSectionHeight() {
    return content.length();
  }

  public void setPAppletAndPGraphics(PGraphics pgDocument, PGraphics pgPreview) {
    this.pgDocument = pgDocument;
    this.pgPreview = pgPreview;
  }
}

